#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <matrix.h>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setDockNestingEnabled(true);
    setWindowTitle("图像处理工具");

    showimage();
    getlabelshow();
    setMouseTracking(true);
    mousepressLabel=new QLabel;//显示鼠标点击时的位置
    mousepressLabel->setText(tr("当前位置"));
    mousepressLabel->setFixedWidth(100);
    mousereleaseLabel=new QLabel;//显示鼠标释放时位置
    mousereleaseLabel->setText(tr("释放位置"));
    mousereleaseLabel->setFixedWidth(100);
    statusBar()->addPermanentWidget(mousepressLabel);
    statusBar()->addPermanentWidget(mousereleaseLabel);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::getlabelshow()//界面设计
{
    dock_light=new QDockWidget(tr("工具栏"), this);

    setCentralWidget(dock_Image);//指定中心窗口
    //窗口布局
    addDockWidget(Qt::LeftDockWidgetArea, dock_light);
    //分割
    //splitDockWidget(dock_status, dock_light, Qt::Horizontal);     //水平
    \
    //splitDockWidget(dock_Image, dock_status, Qt::Vertical);      //垂直
    //合并
    //tabifyDockWidget(dock_light, dock_status);
    \
    //raise()函数使窗口最靠前
    dock_light->raise();
    dock_light->setFixedSize(180,520);
    dock_light->setFeatures(QDockWidget::NoDockWidgetFeatures);

    //在dock_light窗口添加控件
    mouseLabel = new QLabel("",dock_light);                    //显示点击鼠标时坐标
    mouseNowLabel = new QLabel("",dock_light);                  //显示释放鼠标时坐标
    lineedit_light = new QLineEdit(tr(""),dock_light);
    lineedit_gaosi = new QLineEdit(tr(""),dock_light);
    lineedit_zhongzhi = new QLineEdit(tr(""),dock_light);
    lineedit_junzhi = new QLineEdit(tr(""),dock_light);
    lineedit_baohedu = new QLineEdit(tr(""),dock_light);
    lineedit_duibidu = new QLineEdit(tr(""),dock_light);
    lineedit_gama = new QLineEdit(tr(""),dock_light);

    button_light = new QPushButton(tr("亮度调节"),dock_light);//如果想在主窗口创建控件，第二个参数QWidget *parent改为this
    button_gaosi = new QPushButton(tr("高斯滤波"),dock_light);
    button_zhongzhi = new QPushButton(tr("中值滤波"),dock_light);
    button_junzhi = new QPushButton(tr("均值滤波"),dock_light);
    button_baohedu = new QPushButton(tr("饱和度调节"),dock_light);
    button_duibidu = new QPushButton(tr("对比度调节"),dock_light);
    button_gama = new QPushButton(tr("伽马调节"),dock_light);

    horizontalSlider_light = new QSlider(Qt::Horizontal, dock_light);
    horizontalSlider_gaosi = new QSlider(Qt::Horizontal, dock_light);
    horizontalSlider_zhongzhi = new QSlider(Qt::Horizontal, dock_light);
    horizontalSlider_junzhi = new QSlider(Qt::Horizontal, dock_light);
    horizontalSlider_baohedu = new QSlider(Qt::Horizontal, dock_light);
    horizontalSlider_duibidu = new QSlider(Qt::Horizontal, dock_light);
    horizontalSlider_gama = new QSlider(Qt::Horizontal, dock_light);

    setMouseTracking(true);
    mouseLabel->move(5,20);
    mouseLabel->resize(40,25);
    mouseNowLabel->move(110,20);
    mouseNowLabel->resize(40,25);
    mouseLabel->setText(tr("(长按)鼠标坐标："));
    mouseLabel->setFixedWidth(100);
    mouseNowLabel->setText(tr(""));
    mouseNowLabel->setFixedWidth(100);

    lineedit_gama->move(5,110);
    lineedit_gama->resize(100,25);
    lineedit_gama->setPlaceholderText("伽马参数(0~2.2)");
    horizontalSlider_gama->setGeometry(5, 140, 100, 20);
    horizontalSlider_gama->setMaximum(22);
    horizontalSlider_gama->setMinimum(0);//滑块区间
    horizontalSlider_gama->setSliderPosition(10);
    horizontalSlider_gama->setTracking(true);//滑块追踪
    horizontalSlider_gama->setPageStep(1);//设置步长
    button_gama->move(110,110);
    button_gama->resize(70,25);
    connect(button_gama, &QPushButton::clicked, lineedit_gama, [=](){slot_button_gama(lineedit_gama); });
    connect(horizontalSlider_gama, &QSlider::sliderMoved, [=](float value){
        lineedit_gama->setText(QString::number(value/10));
        slot_button_gama(lineedit_gama);
    });

    lineedit_light->move(5,170);
    lineedit_light->resize(100,25);
    lineedit_light->setPlaceholderText("亮度参数(0~100)");
    horizontalSlider_light->setGeometry(5, 200, 100, 20);
    horizontalSlider_light->setMaximum(100);
    horizontalSlider_light->setMinimum(0);//滑块区间
    horizontalSlider_light->setSliderPosition(10);
    horizontalSlider_light->setTracking(true);//滑块追踪
    horizontalSlider_light->setPageStep(1);//设置步长
    button_light->move(110,170);
    button_light->resize(70,25);
    connect(button_light, &QPushButton::clicked, lineedit_light, [=](){slot_button_light(lineedit_light); });
    connect(horizontalSlider_light, &QSlider::sliderMoved, [=](float value){
        lineedit_light->setText(QString::number(value));
        slot_button_light(lineedit_light);
    });

    lineedit_gaosi->move(5,230);
    lineedit_gaosi->resize(100,25);
    lineedit_gaosi->setPlaceholderText("高斯参数(3~5)");
    lineedit_gaosi->setValidator(new QIntValidator(0,11,lineedit_gaosi));
    horizontalSlider_gaosi->setGeometry(5, 260, 100, 20);
    horizontalSlider_gaosi->setMaximum(50);
    horizontalSlider_gaosi->setMinimum(30);//滑块区间
    horizontalSlider_gaosi->setSliderPosition(30);
    horizontalSlider_gaosi->setTracking(true);//滑块追踪
    horizontalSlider_gaosi->setPageStep(1);//设置步长
    button_gaosi->move(110,230);
    button_gaosi->resize(70,25);
    connect(button_gaosi, &QPushButton::clicked, lineedit_gaosi, [=](){slot_button_gaosi(lineedit_gaosi); });
    connect(horizontalSlider_gaosi, &QSlider::sliderMoved, [=](float value){
        lineedit_gaosi->setText(QString::number(value/10));
        slot_button_gaosi(lineedit_gaosi);
    });

    lineedit_zhongzhi->move(5,290);
    lineedit_zhongzhi->resize(100,25);
    lineedit_zhongzhi->setPlaceholderText("中值滤波(2~4)");
    lineedit_zhongzhi->setValidator(new QIntValidator(0,11,this));
    horizontalSlider_zhongzhi->setGeometry(5, 320, 100, 20);
    horizontalSlider_zhongzhi->setMaximum(40);
    horizontalSlider_zhongzhi->setMinimum(20);//滑块区间
    horizontalSlider_zhongzhi->setSliderPosition(30);
    horizontalSlider_zhongzhi->setTracking(true);//滑块追踪
    horizontalSlider_zhongzhi->setPageStep(1);//设置步长
    button_zhongzhi->move(110,290);
    button_zhongzhi->resize(70,25);
    connect(button_zhongzhi, &QPushButton::clicked, lineedit_zhongzhi, [=](){slot_button_zhongzhi(lineedit_zhongzhi); });
    connect(horizontalSlider_zhongzhi, &QSlider::sliderMoved, [=](float value){
        lineedit_zhongzhi->setText(QString::number(value/10));
        slot_button_zhongzhi(lineedit_zhongzhi);
    });

    lineedit_junzhi->move(5,350);
    lineedit_junzhi->resize(100,25);
    lineedit_junzhi->setPlaceholderText("均值滤波(2~4)");
    lineedit_junzhi->setValidator(new QIntValidator(0,11,this));
    horizontalSlider_junzhi->setGeometry(5, 380, 100, 20);
    horizontalSlider_junzhi->setMaximum(40);
    horizontalSlider_junzhi->setMinimum(20);//滑块区间
    horizontalSlider_junzhi->setSliderPosition(30);
    horizontalSlider_junzhi->setTracking(true);//滑块追踪
    horizontalSlider_junzhi->setPageStep(1);//设置步长
    button_junzhi->move(110,350);
    button_junzhi->resize(70,25);
    connect(button_junzhi, &QPushButton::clicked, lineedit_junzhi, [=](){slot_button_junzhi(lineedit_junzhi); });
    connect(horizontalSlider_junzhi, &QSlider::sliderMoved, [=](float value){
        lineedit_junzhi->setText(QString::number(value/10));
        slot_button_junzhi(lineedit_junzhi);
    });

    lineedit_baohedu->move(5,410);
    lineedit_baohedu->resize(100,25);
    lineedit_baohedu->setPlaceholderText("输入范围-1到1");
    lineedit_baohedu->setValidator(new QDoubleValidator(-1.0,1.0,6,this));
    horizontalSlider_baohedu->setGeometry(5, 440, 100, 20);
    horizontalSlider_baohedu->setMaximum(100);
    horizontalSlider_baohedu->setMinimum(-100);//滑块区间
    horizontalSlider_baohedu->setSliderPosition(0);
    horizontalSlider_baohedu->setTracking(true);//滑块追踪
    horizontalSlider_baohedu->setPageStep(1);//设置步长
    button_baohedu->move(110,410);
    button_baohedu->resize(70,25);
    connect(button_baohedu, &QPushButton::clicked, lineedit_baohedu, [=](){slot_button_baohedu(lineedit_baohedu); });
    connect(horizontalSlider_baohedu, &QSlider::sliderMoved, [=](float value){
        lineedit_baohedu->setText(QString::number(value/100));
        slot_button_baohedu(lineedit_baohedu);
    });

    lineedit_duibidu->move(5,470);
    lineedit_duibidu->resize(100,25);
    lineedit_duibidu->setPlaceholderText("范围-255到255");
    lineedit_duibidu->setValidator(new QDoubleValidator(-255,255,6,this));
    horizontalSlider_duibidu->setGeometry(5, 500, 100, 20);
    horizontalSlider_duibidu->setMaximum(255);
    horizontalSlider_duibidu->setMinimum(-255);//滑块区间
    horizontalSlider_duibidu->setSliderPosition(0);
    horizontalSlider_duibidu->setTracking(true);//滑块追踪
    horizontalSlider_duibidu->setPageStep(1);//设置步长
    button_duibidu->move(110,470);
    button_duibidu->resize(70,25);
    connect(button_duibidu, &QPushButton::clicked, lineedit_duibidu, [=](){slot_button_duibidu(lineedit_duibidu); });
    connect(horizontalSlider_duibidu, &QSlider::sliderMoved, [=](float value){
        lineedit_duibidu->setText(QString::number(value));
        slot_button_duibidu(lineedit_duibidu);
    });

}

void MainWindow::showimage() //初始化图像
{
    setMouseTracking(true);
    dock_Image = new QDockWidget(tr("图像栏"), this);
    setCentralWidget(dock_Image);

    imgLabel = new QLabel(dock_Image);
    imgLabel->setScaledContents(true);
    imgLabel->setMouseTracking(true);//设置窗体追踪鼠标
    dock_Image->setFeatures(QDockWidget::NoDockWidgetFeatures);


    QImage image = QImage(600, 450, QImage::Format_RGB888);
    image.fill(qRgb(255, 255, 255));
    imgLabel->setPixmap(QPixmap::fromImage(image));         // 显示图像
    imgLabel->resize(image.width(), image.height());

    QScrollArea* scrollArea = new QScrollArea(this);
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setAlignment(Qt::AlignCenter);
    scrollArea->setWidget(imgLabel);
    dock_Image->setWidget(scrollArea);
}

void MainWindow::mousePressEvent(QMouseEvent *event)//鼠标点击事件
{
    //x1,y1为以label控件左上角为坐标原点获取lablel控件上的鼠标位置
    double x1=event->x()-imgLabel->x()-185;
    double y1=event->y()-imgLabel->y();
    //x2,y2为以label控件左下角为坐标原点获取lablel控件上的鼠标位置
    double x2=x1;
    double y2=imgLabel->height()-y1+93-15;
    p_press=QPointF(x2,y2);
    mousepressLabel->setText("("+QString::number(x2)+","+QString::number(y2)+")");

}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)//鼠标释放事件
{
    //x1,y1为以label控件左上角为坐标原点获取lablel控件上的鼠标位置
    double x1=event->x()-imgLabel->x()-185;
    double y1=event->y()-imgLabel->y();
    //x2,y2为以label控件左下角为坐标原点获取lablel控件上的鼠标位置
    double x2=x1;
    double y2=imgLabel->height()-y1+93-15;
    p_release=QPointF(x2,y2);
    mousereleaseLabel->setText("("+QString::number(x2)+","+QString::number(y2)+")");
}

void MainWindow::mouseMoveEvent(QMouseEvent *ev){
    setMouseTracking(true);
    //x1,y1为以label控件左上角为坐标原点获取lablel控件上的鼠标位置
    double x1=ev->x()-imgLabel->x()-185;
    double y1=ev->y()-imgLabel->y();
    //x2,y2为以label控件左下角为坐标原点获取lablel控件上的鼠标位置
    double x2=x1;
    double y2=imgLabel->height()-y1+93-15;
    mouseNowLabel->setText("("+QString::number(x2)+","+QString::number(y2)+")");
}

void MainWindow::wheelEvent(QWheelEvent *event)//鼠标滚轮事件
{
    wheeldelta=event->angleDelta();
    if (!wheeldelta.isNull())//判断滚轮是否转动
    {
        step = wheeldelta.y();//将滚轮转动数值传给中间p
        int imgwidth=imgLabel->width();
        int imgheight=imgLabel->height();

        if(currentPath.isEmpty())
        {
            event->accept();
        }
        else
        {
            if(imgheight<=200||imgwidth<=200)
            {
                if(step<0)
                {
                    event->accept();
                }
                else
                {
                    imgwidth += step;
                    imgheight += step;
                    a.Resize(imgheight,imgwidth);
                    delete img;
                    img = new QImage(a.width,a.height,QImage::Format_RGB32);
                    a.data=(*img).bits();
                    a.doubletosingle();
                    a.data=img->bits();
                    imgLabel->setPixmap(QPixmap::fromImage(*img));
                    imgLabel->resize(img->width(), img->height());
                    if(!flagroll)
                    {
                        baocun();
                        addimg.copy(a);
                        flagroll=1;
                    }
                   else
                    {

                           if(nowimgstep>=30)
                           {
                               delete b[0];
                               b[0]=nullptr;
                               for(int i=1;i<30;i++)
                               {
                                   b[i-1]=b[i];
                               }
                               nowimgstep=30-1;
                               b[nowimgstep]=new Matrix;
                           }
                           else
                           {
                               b[nowimgstep]=new Matrix;
                           }
                          (*b[nowimgstep]).copy(a);
                           reimgstep=0;
                        addimg.copy(a);
                        flagroll=1;
                    }

                }
        }
        else if(imgheight >=2000||imgwidth>=2000)
        {
            if(step>0)
            {
                event->accept();
            }
            else
            {
                imgwidth += step;
                imgheight += step;
                a.Resize(imgheight,imgwidth);
                delete img;
                img = new QImage(a.width,a.height,QImage::Format_RGB32);
                a.data=(*img).bits();
                a.doubletosingle();
                a.data=img->bits();
                imgLabel->setPixmap(QPixmap::fromImage(*img));
                imgLabel->resize(img->width(), img->height());
                if(!flagroll)
                {
                    baocun();
                    addimg.copy(a);
                    flagroll=1;
                }
               else
                {

                       if(nowimgstep>=30)
                       {
                           delete b[0];
                           b[0]=nullptr;
                           for(int i=1;i<30;i++)
                           {
                               b[i-1]=b[i];
                           }
                           nowimgstep=30-1;
                           b[nowimgstep]=new Matrix;
                       }
                       else
                       {
                           b[nowimgstep]=new Matrix;
                       }
                      (*b[nowimgstep]).copy(a);
                       reimgstep=0;
                    addimg.copy(a);
                    flagroll=1;
                }

            }
        }
        else
        {
            imgwidth += step;
            imgheight += step;
            a.Resize(imgheight,imgwidth);
            delete img;
            img = new QImage(a.width,a.height,QImage::Format_RGB32);
            a.data=(*img).bits();
            a.doubletosingle();
            a.data=img->bits();
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            if(!flagroll)
            {
                baocun();
                addimg.copy(a);
                flagroll=1;
            }
           else
            {

                   if(nowimgstep>=30)
                   {
                       delete b[0];
                       b[0]=nullptr;
                       for(int i=1;i<30;i++)
                       {
                           b[i-1]=b[i];
                       }
                       nowimgstep=30-1;
                       b[nowimgstep]=new Matrix;
                   }
                   else
                   {
                       b[nowimgstep]=new Matrix;
                   }
                  (*b[nowimgstep]).copy(a);
                   reimgstep=0;
                addimg.copy(a);
                flagroll=1;
            }

        }
    }

    event->accept();
}
}

void MainWindow::baocun()//撤销副本
{
    nowimgstep++;
       if(nowimgstep>=30)
       {
           delete b[0];
           b[0]=nullptr;
           for(int i=1;i<30;i++)
           {
               b[i-1]=b[i];
           }
           nowimgstep=30-1;
           b[nowimgstep]=new Matrix;
       }
       else
       {
           b[nowimgstep]=new Matrix;
       }
      (*b[nowimgstep]).copy(a);
       reimgstep=0;
       flagroll=0;
}

void MainWindow::on_actionopenfile_triggered() //打开文件
{
    if(img!=nullptr)
    {
        delete img;
    }
    QString path = QFileDialog::getOpenFileName(this, tr("选择图像"), ".", tr("Images(*.jpg *.png *.bmp)"));// 文件选择框
    if (!path.isEmpty())
    {
        a.Readimage(path);
        //QImage b(a.width,a.height ,QImage::Format_RGB32);
        img=new QImage (a.width,a.height ,QImage::Format_RGB32);
        a.data=(*img).bits();
        a.doubletosingle();
        a.data=(*img).bits();
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->resize(img->width(), img->height());
        currentPath = path;
        baocun();
        addimg.copy(a);
    }

}

void MainWindow::on_actionsave_triggered() //文件保存
{
    QString file_path = currentPath;
        if(file_path.isEmpty())

        {
            a.Writeimage("D:\\Saveimp.bmp");
            return;
        }
       a.Writeimage(file_path);
        a.data=(*img).bits();
}

void MainWindow::on_actionsaveas_triggered() //文件另存为
{
    QString file_path = QFileDialog::getSaveFileName(this, tr("另存为"), ".", tr("Images(*.jpg *.png *.bmp)"));

        if(file_path.isEmpty())

        {
            a.Writeimage("D:\\progress\\fur2.bmp");
            return;
        }
       a.Writeimage(file_path);
        a.data=(*img).bits();
}

void MainWindow::slot_button_light(QLineEdit *lineedit_light) //亮度侧栏调节
{
    if(!currentPath.isEmpty())
    {
        QString light0=lineedit_light->text();
        double light1=light0.toDouble();
        this->horizontalSlider_light->setValue(light1);
        double light=light0.toDouble();
        a.copy(addimg);
        a.light(light/10);
        delete img;
        img = new QImage(a.width,a.height,QImage::Format_RGB32);
        a.data=(*img).bits();
        a.doubletosingle();
        a.data=img->bits();
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->resize(img->width(), img->height());
        baocun();
    }
}

void MainWindow::slot_button_gaosi(QLineEdit *lineedit_gaosi)//高斯滤波
{
    if(!currentPath.isEmpty())
    {
        QString ksize0=lineedit_gaosi->text();
        double ksize1=ksize0.toDouble();
        this->horizontalSlider_gaosi->setValue(ksize1*10);
        int ksize=ksize0.toInt();
        if(ksize>0&&ksize<=a.width&&ksize<=a.height)
        {
            a.copy(addimg);
            a.gaosilvboqi(ksize,0.8);
            delete img;
            img = new QImage(a.width,a.height,QImage::Format_RGB32);
            a.data=(*img).bits();
            a.doubletosingle();
            a.data=img->bits();
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            baocun();
        }
    }
}

void MainWindow::slot_button_zhongzhi(QLineEdit *lineedit_zhongzhi)//中值滤波
{
    if(!currentPath.isEmpty())
    {
        QString ksize0=lineedit_zhongzhi->text();
        double ksize1=ksize0.toDouble();
        this->horizontalSlider_zhongzhi->setValue(ksize1*10);
        int ksize=ksize0.toInt();
        if(ksize>0&&ksize<=a.width&&ksize<=a.height)
        {
            a.copy(addimg);
            a.zhongzhilvboqi(ksize);
            delete img;
            img = new QImage(a.width,a.height,QImage::Format_RGB32);
            a.data=(*img).bits();
            a.doubletosingle();
            a.data=img->bits();
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            baocun();
        }
    }
}

void MainWindow::slot_button_junzhi(QLineEdit *lineedit_junzhi)//均值滤波
{
    if(!currentPath.isEmpty())
    {
        QString ksize0=lineedit_junzhi->text();
        double ksize1=ksize0.toDouble();
        this->horizontalSlider_junzhi->setValue(ksize1*10);
        int ksize=ksize0.toInt();
        if(ksize>0&&ksize<=a.width&&ksize<=a.height)
        {
            a.copy(addimg);
            a.junzhilvboqi(ksize);
            delete img;
            img = new QImage(a.width,a.height,QImage::Format_RGB32);
            a.data=(*img).bits();
            a.doubletosingle();
            a.data=img->bits();
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            baocun();
        }
    }
}

void MainWindow::slot_button_baohedu(QLineEdit *lineedit_baohedu)//饱和度调节
{
    if(!currentPath.isEmpty())
    {
        QString percent0=lineedit_baohedu->text();
        double percent1=percent0.toDouble();
        this->horizontalSlider_baohedu->setValue(percent1*100);
        double percent=percent0.toDouble();
        if(percent>=-1.0 && percent<=1.0)
        {
            a.copy(addimg);
            a.baohedu(percent);
            delete img;
            img = new QImage(a.width,a.height,QImage::Format_RGB32);
            a.data=(*img).bits();
            a.doubletosingle();
            a.data=img->bits();
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            baocun();
        }
    }
}

void MainWindow::slot_button_duibidu(QLineEdit *lineedit_duibidu)//对比度调节
{
    if(!currentPath.isEmpty())
    {
        QString t0=lineedit_duibidu->text();
        double t1=t0.toDouble();
        this->horizontalSlider_duibidu->setValue(t1);
        double t=t0.toDouble();
        if(t>=-255.0 && t<=255.0)
        {
            a.copy(addimg);
            a.duibidu(t);
            delete img;
            img = new QImage(a.width,a.height,QImage::Format_RGB32);
            a.data=(*img).bits();
            a.doubletosingle();
            a.data=img->bits();
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            baocun();
        }
    }
}

void MainWindow::slot_button_gama(QLineEdit *lineedit_gama)//对比度调节
{
    if(!currentPath.isEmpty())
    {
        QString gama=lineedit_gama->text();
        double gama1=gama.toDouble();
        this->horizontalSlider_gama->setValue(gama1*10);
        double gamma=gama.toDouble();
        a.copy(addimg);
        a.gamma(gamma);
        delete img;
        img = new QImage(a.width,a.height,QImage::Format_RGB32);
        a.data=(*img).bits();
        a.doubletosingle();
        a.data=img->bits();
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->resize(img->width(), img->height());
        baocun();
    }
}

void MainWindow::on_actionflip_ud_triggered() //上下翻转
{
    if(!currentPath.isEmpty())
    {
        a.Flip(1);
        delete img;
        img = new QImage(a.width,a.height,QImage::Format_RGB32);
        a.data=(*img).bits();
        a.doubletosingle();
        a.data=img->bits();
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->resize(img->width(), img->height());
        baocun();
        addimg.copy(a);
    }
}

void MainWindow::on_actionflip_lr_triggered() //左右翻转
{
    if(!currentPath.isEmpty())
    {
        a.Flip(0);
        delete img;
        img = new QImage(a.width,a.height,QImage::Format_RGB32);
        a.data=(*img).bits();
        a.doubletosingle();
        a.data=img->bits();
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->resize(img->width(), img->height());
        baocun();
        addimg.copy(a);
    }
}

void MainWindow::on_actioncut_triggered() //剪切
{
    if(!currentPath.isEmpty())
    {
        int x1, y1, x2, y2;
        x1=p_press.rx();
        y1=p_press.ry();
        x2=p_release.rx();
        y2=p_release.ry();
        if(!(x1<0||x2<0||y1<0||y2<0||x1>a.width||x2>a.width||y1>a.height||y2>a.height))
        {
            if(!(x1==x2&&y1==y2))
            {
                a.Cut(x1,y1,x2,y2);
                delete img;
                img = new QImage(a.width,a.height,QImage::Format_RGB32);
                a.data=(*img).bits();
                a.doubletosingle();
                a.data=img->bits();
                imgLabel->setPixmap(QPixmap::fromImage(*img));
                imgLabel->resize(img->width(), img->height());
                p_press.setX(0);
                p_press.setY(0);
                p_release.setX(0);
                p_release.setY(0);
                baocun();
                addimg.copy(a);
            }
        }
    }
}

void MainWindow::on_actionmosaic_triggered() //马赛克
{
    if(!currentPath.isEmpty())
    {
        int x1, y1, x2, y2;
        x1=p_press.rx();
        y1=p_press.ry();
        x2=p_release.rx();
        y2=p_release.ry();
        if(!(x1<0||x2<0||y1<0||y2<0||x1>a.width||x2>a.width||y1>a.height||y2>a.height))
        {
            if(x1!=0||x2!=0||y1!=0||y2!=0)
            {
                a.mosaic(y1, x1, y2, x2,8);
                delete img;
                img = new QImage(a.width,a.height,QImage::Format_RGB32);
                a.data=(*img).bits();
                a.doubletosingle();
                a.data=img->bits();
                imgLabel->setPixmap(QPixmap::fromImage(*img));
                imgLabel->resize(img->width(), img->height());
                baocun();
                addimg.copy(a);
            }
        }

    }
}

void MainWindow::on_actiongrey_triggered() //灰度
{
    if(!currentPath.isEmpty())
    {
        a.copy(addimg);
        a.grey();
        delete img;
        img = new QImage(a.width,a.height,QImage::Format_RGB32);
        a.data=(*img).bits();
        a.doubletosingle();
        a.data=img->bits();
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->resize(img->width(), img->height());
        baocun();
    }
}

void MainWindow::on_actionlight_triggered()//亮度工具栏按钮调节
{
    if(!currentPath.isEmpty())
    {
        bool ok = false;
        QString dlgTitle=QStringLiteral("light");//对话框标题
        QString txtLabel=QStringLiteral("输入亮度参数:");//对话框Label显示内容
        int defaultInput=10;
        int minValue=0,maxValue=100,stepValue=1;
        int inputValue=QInputDialog::getInt(this,dlgTitle,txtLabel, defaultInput,minValue,maxValue,stepValue,&ok);
        if(ok){
            this->lineedit_light->setText(QString::number(inputValue));
            this->horizontalSlider_light->setValue(inputValue);
            slot_button_light(lineedit_light);
        }
    }
}

void MainWindow::on_actionhelp_triggered()//程序说明
{
    QString helppath = QDir::currentPath();

     QDir *dir = new QDir(helppath);
    dir->cdUp();
    //helppath = dir->dirName();
    helppath=dir->path();
    QDesktopServices::openUrl(QUrl("file:///"+helppath+"/README.md"));
}

void MainWindow::on_actionresize_triggered()//缩放工具栏按钮调节，手动传参
{
    if(!currentPath.isEmpty())
    {
        QDialog dialog(this);
        QFormLayout form(&dialog);
        //form.addRow(new QLabel("User input:"));//对话框提示
        //height
        QString height = QString("高度: ");//标题
        QSpinBox *spinbox1 = new QSpinBox(&dialog);
        spinbox1->setRange(0, 2000);//变化范围
        form.addRow(height, spinbox1);
        //width
        QString width = QString("宽度: ");//标题
        QSpinBox *spinbox2 = new QSpinBox(&dialog);
        spinbox2->setRange(0, 2000);//变化范围
        form.addRow(width, spinbox2);

        //按钮设置
        QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,Qt::Horizontal, &dialog);
        form.addRow(&buttonBox);
        QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
        QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

        //ok的槽函数
        if (dialog.exec() == QDialog::Accepted)
        {
            int h = spinbox1->value();
            int w = spinbox2->value();
            a.Resize(h,w);
            delete img;
            img = new QImage(a.width,a.height,QImage::Format_RGB32);
            a.data=(*img).bits();
            a.doubletosingle();
            a.data=img->bits();
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            baocun();
            addimg.copy(a);
        }
    }
}

void MainWindow::on_actionbackstep_triggered()//撤回
{
    if(!currentPath.isEmpty())
    {
        if(nowimgstep<=0)
            {
                return ;
            }
            nowimgstep--;
            a.copy(*b[nowimgstep]);
            delete img;
            img= new QImage (a.width,a.height ,QImage::Format_RGB32);
            a.data=(*img).bits();
            a.doubletosingle();
            a.data=(*img).bits();
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            reimgstep++;
    }
}

void MainWindow::on_actiongamma_triggered()//伽马矫正
{
    if(!currentPath.isEmpty())
    {
        getlabel = new QLabel(tr("gamma"));
        bool ok;
            double stepValue=0.1;
            double gamma=QInputDialog::getDouble(this,tr("伽马调节"),tr("输入0到2.2的值"),1.0,0,2.2,1,&ok,Qt::WindowFlags(),stepValue);

            if (ok)
            {
                getlabel->setText(QString(tr("%1")).arg(gamma));//QString::arg()函数用第一个arg()调用会替换“％1”，第2个arg()调用会替换“％2”。上面的
            }
            a.copy(addimg);
        a.gamma(gamma);
        delete img;
        img = new QImage(a.width,a.height,QImage::Format_RGB32);
        a.data=(*img).bits();
        a.doubletosingle();
        a.data=img->bits();
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->resize(img->width(), img->height());
        baocun();
    }
}

void MainWindow::on_actionruihua_triggered()//锐化
{
    if(!currentPath.isEmpty())
    {
        QDialog dialog(this);
        QFormLayout form(&dialog);
        //t
        QString t = QString("锐化强度: ");//标题
        QDoubleSpinBox *spinbox1 = new QDoubleSpinBox(&dialog);
        spinbox1->setRange(0, 255);//变化范围
        form.addRow(t, spinbox1);
        //lownumber
        QString lownumber = QString("低阈值: ");//标题
        QDoubleSpinBox *spinbox2 = new QDoubleSpinBox(&dialog);
        spinbox2->setRange(0, 255);//变化范围
        form.addRow(lownumber, spinbox2);
        //highnumber
        QString highnumber = QString("高阈值: ");//标题
        QDoubleSpinBox *spinbox3 = new QDoubleSpinBox(&dialog);
        spinbox3->setRange(0, 255);//变化范围
        form.addRow(highnumber, spinbox3);
        //ksize
        QString ksize = QString("滤波器边长: ");//标题
        QSpinBox *spinbox4 = new QSpinBox(&dialog);
        spinbox4->setRange(0, 11);//变化范围
        form.addRow(ksize, spinbox4);
        //strength
        QString strength = QString("滤波强度: ");//标题
        QDoubleSpinBox *spinbox5 = new QDoubleSpinBox(&dialog);
        spinbox5->setRange(0.7,0.9);//变化范围
        form.addRow(strength, spinbox5);
        //按钮设置
        QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,Qt::Horizontal, &dialog);
        form.addRow(&buttonBox);
        QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
        QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

        //ok的槽函数
        if (dialog.exec() == QDialog::Accepted)
        {
           double t=spinbox1->value();
           double lownumber=spinbox2->value();
           double highnumber=spinbox3->value();
           int ksize=spinbox4->value();
           double strength=spinbox5->value();
           a.copy(addimg);
           a.ruihua(t,lownumber,highnumber,ksize,strength);
           delete img;
           img = new QImage(a.width,a.height,QImage::Format_RGB32);
           a.data=(*img).bits();
           a.doubletosingle();
           a.data=img->bits();
           imgLabel->setPixmap(QPixmap::fromImage(*img));
           imgLabel->resize(img->width(), img->height());
           baocun();
        }
    }
}

void MainWindow::on_actionbytq_triggered()//边缘提取
{
    if(!currentPath.isEmpty())
    {
        QDialog dialog(this);
        QFormLayout form(&dialog);
        //t
        QString t = QString("锐化强度: ");//标题
        QDoubleSpinBox *spinbox1 = new QDoubleSpinBox(&dialog);
        spinbox1->setRange(0, 255);//变化范围
        form.addRow(t, spinbox1);
        //lownumber
        QString lownumber = QString("低阈值: ");//标题
        QDoubleSpinBox *spinbox2 = new QDoubleSpinBox(&dialog);
        spinbox2->setRange(0, 255);//变化范围
        form.addRow(lownumber, spinbox2);
        //highnumber
        QString highnumber = QString("高阈值: ");//标题
        QDoubleSpinBox *spinbox3 = new QDoubleSpinBox(&dialog);
        spinbox3->setRange(0, 255);//变化范围
        form.addRow(highnumber, spinbox3);
        //ksize
        QString ksize = QString("滤波器边长: ");//标题
        QSpinBox *spinbox4 = new QSpinBox(&dialog);
        spinbox4->setRange(0, 11);//变化范围
        form.addRow(ksize, spinbox4);
        //strength
        QString strength = QString("滤波强度: ");//标题
        QDoubleSpinBox *spinbox5 = new QDoubleSpinBox(&dialog);
        spinbox5->setRange(0.7,0.9);//变化范围
        form.addRow(strength, spinbox5);
        //按钮设置
        QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,Qt::Horizontal, &dialog);
        form.addRow(&buttonBox);
        QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
        QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

        //ok的槽函数
        if (dialog.exec() == QDialog::Accepted)
        {
           double t=spinbox1->value();
           double lownumber=spinbox2->value();
           double highnumber=spinbox3->value();
           int ksize=spinbox4->value();
           double strength=spinbox5->value();
           a.copy(addimg);
           a.bianyuantiqu(t,lownumber,highnumber,ksize,strength);
           delete img;
           img = new QImage(a.width,a.height,QImage::Format_RGB32);
           a.data=(*img).bits();
           a.doubletosingle();
           a.data=img->bits();
           imgLabel->setPixmap(QPixmap::fromImage(*img));
           imgLabel->resize(img->width(), img->height());
           baocun();
        }
    }
}

void MainWindow::on_actionahead_triggered()//返回撤销
{
    if(!currentPath.isEmpty())
    {
        if(reimgstep<=0)
            {
                return ;
            }
            nowimgstep++;
            a.copy(*b[nowimgstep]);
            delete img;
            img= new QImage (a.width,a.height ,QImage::Format_RGB32);
            a.data=(*img).bits();
            a.doubletosingle();
            a.data=(*img).bits();
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            reimgstep--;
    }
}

void MainWindow::on_actionrotate0_triggered()//逆时针旋转
{
    if(!currentPath.isEmpty())
    {
        a.Rotate(1);
        delete img;
        img = new QImage(a.width,a.height,QImage::Format_RGB32);
        a.data=(*img).bits();
        a.doubletosingle();
        a.data=img->bits();
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->resize(img->width(), img->height());
        baocun();
        addimg.copy(a);
    }
}

void MainWindow::on_actionrotate1_triggered()//顺时针转动
{
    if(!currentPath.isEmpty())
    {
        a.Rotate(0);
        delete img;
        img = new QImage(a.width,a.height,QImage::Format_RGB32);
        a.data=(*img).bits();
        a.doubletosingle();
        a.data=img->bits();
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->resize(img->width(), img->height());
        baocun();
        addimg.copy(a);
    }
}

